﻿using System;

namespace Useful.MVP
{
    public class BasePresenter<M, IV, R>
        where M : class, new()
        where IV : IView
    {
        protected readonly M _Model = new M();
        protected readonly IV _View;
        protected R _Repository;
        protected EventHandler<ExceptionEventArgs> _ExceptionHandler;

        protected BasePresenter(IV view, R repository)
        {
            _Repository = repository;

            _View = view;

            _View.ExceptionRaised += _View_ExceptionRaised;
        }

        protected void HandleException(string msg, Exception ex)
        {
            _ExceptionHandler(this, new ExceptionEventArgs(msg, ex));
        }

        private void _View_ExceptionRaised(object sender, ExceptionEventArgs e)
        {
            _ExceptionHandler(this, e);
        }

        protected void Init(R repository, EventHandler<ExceptionEventArgs> exceptionEvent)
        {
            _Repository = repository;
            _ExceptionHandler = exceptionEvent;
        }
    }
}
